package com.ihighest.app.infox.adapter;

import org.json.JSONArray;
import org.json.JSONException;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.infox.R;

public class EventAdapter extends BaseAdapter {
	private JSONArray jsonArray;
	private LayoutInflater inflater;
	private Context context;

	public EventAdapter(Context context) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return jsonArray == null ? 0 : jsonArray.length();
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View contentView, ViewGroup parent) {
		ViewHolder holder;
		if (contentView == null) {
			holder = new ViewHolder();
			contentView = inflater.inflate(R.layout.infox_activity_event_item, null);
			holder.title = (TextView) contentView.findViewById(R.id.eventItemTitle);
			holder.date = (TextView) contentView.findViewById(R.id.eventItemDate);
			holder.content = (TextView) contentView.findViewById(R.id.eventItemContent);
			holder.type = (TextView) contentView.findViewById(R.id.eventItemType);
			// holder.haveData = (RelativeLayout)
			// contentView.findViewById(R.id.haveData);
			// holder.noDate = (TextView) contentView.findViewById(R.id.noData);
			contentView.setTag(holder);

		} else {
			holder = (ViewHolder) contentView.getTag();
		}
		// if (jsonArray != null) {
		// holder.haveData.setVisibility(View.VISIBLE);
		// holder.noDate.setVisibility(View.GONE);
		if (position % 2 == 0)
			contentView.setBackgroundColor(context.getResources().getColor(R.color.gray_backgroud));
		else
			contentView.setBackgroundColor(context.getResources().getColor(R.color.white_color));
		try {
			holder.title.setText(jsonArray.getJSONObject(position).getString("title"));
			holder.date.setText(AppKit.timeToBeforeStr(jsonArray.getJSONObject(position).getLong("time")));
			holder.content.setText(jsonArray.getJSONObject(position).getString("memo"));
			if (jsonArray.getJSONObject(position).getString("state").equals("Running")) {
				if (jsonArray.getJSONObject(position).getString("join").equals("yes")) {
					holder.type.setText("已参与");
					holder.type.setTextColor(context.getResources().getColor(R.color.event_takePartIn));
					holder.type.setBackgroundResource(R.drawable.infox_event_takepartin);
				} else {
					holder.type.setText("进行中");
					holder.type.setTextColor(context.getResources().getColor(R.color.event_inprovess));
					holder.type.setBackgroundResource(R.drawable.infox_event_inprocess);
				}
			} else if (jsonArray.getJSONObject(position).getString("state").equals("Joined")) {
				holder.type.setText("已参与");
				holder.type.setTextColor(context.getResources().getColor(R.color.event_takePartIn));
				holder.type.setBackgroundResource(R.drawable.infox_event_takepartin);
			} else if (jsonArray.getJSONObject(position).getString("state").equals("Finished")) {
				if (jsonArray.getJSONObject(position).getString("join").equals("yes")) {
					holder.type.setText("已参与");
					holder.type.setTextColor(context.getResources().getColor(R.color.event_takePartIn));
					holder.type.setBackgroundResource(R.drawable.infox_event_takepartin);
				} else {
					holder.type.setText("已结束");
					holder.type.setTextColor(context.getResources().getColor(R.color.event_finish));
					holder.type.setBackgroundResource(R.drawable.infox_event_finish);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		// } else {
		// holder.haveData.setVisibility(View.GONE);
		// holder.noDate.setVisibility(View.VISIBLE);
		// }
		return contentView;
	}

	class ViewHolder {
		// RelativeLayout haveData;
		// TextView noDate;
		TextView title, date, content, type;
	}

	/**
	 * 替换当前数据源数据
	 * 
	 * @param moreArry
	 * 
	 * @author lkx Created at 2015-10-20 下午8:49:23
	 */
	public void setData(JSONArray arr) {
		this.jsonArray = arr;
		notifyDataSetChanged();
	}

	/**
	 * 向已有数据源中追加数据
	 * 
	 * @param jsonArray
	 * 
	 * @author lkx Created at 2015-10-20 下午8:49:00
	 */
	public void appendData(JSONArray arr) {
		try {
			jsonArray = AppKit.joinJSONArray(jsonArray, arr);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		notifyDataSetChanged();
	}
}
